package com.schoolcloud.share.controller;

import com.schoolcloud.common.model.PageDTO;
import com.schoolcloud.common.model.R;
import com.schoolcloud.share.entity.Tags;
import com.schoolcloud.share.entity.VideoTags;
import com.schoolcloud.share.query.TagsQuery;
import com.schoolcloud.share.service.TagsService;
import com.schoolcloud.share.service.VideoTagsService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/share/tag")
@RequiredArgsConstructor
public class ShareTagController {
    private final TagsService tagsService;
    @GetMapping("/page")
    public R<PageDTO<Tags>> getTagPage(TagsQuery query) {
        return R.ok(tagsService.getTagPage(query));
    }
    @GetMapping("/list")
    public R<List<Tags>> getTagList(TagsQuery query) {
        String name = query.getName();
        List<Tags> list = tagsService.lambdaQuery().like(name != null, Tags::getName, name).list();
        return R.ok(list);
    }
    @PostMapping
    public R<?> save(@RequestBody Tags tag) {
        return tagsService.save(tag) ? R.ok() : R.err("新增失败");
    }
    @PutMapping
    public R<?> update(@RequestBody Tags tag) {
        return tagsService.updateById(tag) ? R.ok() : R.err("更新失败");
    }
    @GetMapping("/{id}")
    public R<Tags> get(@PathVariable Integer id) {
        return R.ok(tagsService.getById(id));
    }
    @DeleteMapping("/{id}")
    public R<?> delete(@PathVariable Integer id) {
        return tagsService.removeById(id) ? R.ok() : R.err("删除失败");
    }
}
